<?php

namespace App\SupplyBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use App\DashboardBundle\Entity\Supply;
use App\DashboardBundle\Entity\ProductKey;


class SupplyController extends Controller
{
    public function indexAction()
    {
        $today = date('Y-m-d');    
        $shopArray = $this->getDoctrine()->getManager()->getRepository('AppDashboardBundle:Shop')->findAll();        
        $productKeyArray = $this->getDoctrine()->getManager()->getRepository('AppDashboardBundle:ProductKey')->findAll();   
        
        $rummageProductArray = null;
        
        return $this->render('AppSupplyBundle:Supply:index.html.twig', array(
            'today' => $today,
            'shopArray' => $shopArray,
            'productKeyArray' => $productKeyArray,
            'rummageProductArray' => $rummageProductArray
        ));
    }
    
    public function listAction()
    {
      $currentYear = date('Y');
      $currentMonth = date('m');
      $currentDay = date('d');
      if(isset($_POST['beginDate'])){
        $beginDate = (string) $_POST['beginDate'];
      }else{
        $beginDate = $currentYear.'-'.$currentMonth.'-01';
      }
      
      if(isset($_POST['endDate'])){
        $endDate = (string) $_POST['endDate'];
      }else{
        $endDate = $currentYear.'-'.$currentMonth.'-'.$currentDay;
        
      }
      
      $supplyArray = $this->getDoctrine()->getManager()->getRepository('AppDashboardBundle:Supply')->findSupplyByDate($beginDate, $endDate);
      
      return $this->render('AppSupplyBundle:Supply:list.html.twig', array(
                  'beginDate' => $beginDate,
                  'endDate' => $endDate,                  
                  'supplyArray' => $supplyArray
              ));
    }
    
    public function saveSupplyAction()
    {
      $date = $_POST['date'];
      $shopId = $_POST['shopId'];
      $hasSupply = $_POST['hasSupply'];
      $productKeys = $_POST['productKeys'];
      $quantity = $_POST['quantity'];
      $per_price = $_POST['per_price'];
      
      // find shop
      $shop = $this->getDoctrine()->getManager()->getRepository('AppDashboardBundle:Shop')->find($shopId);
      
      
      $oldRummage = 0;
      $oldPerPrice = 0;
      $oldTotal = 0;
      
      foreach ($productKeys as $k=>$v)
      {
        
        $oneProductKey = $this->getDoctrine()->getManager()->getRepository('AppDashboardBundle:ProductKey')->find((int) $v);
        
        $oldSupply = $this->getDoctrine()->getManager()->getRepository('AppDashboardBundle:Supply')->findBy(array('shop' => $shop, 'productKey' => $oneProductKey, 'isActive' => 1));        
        
        if($oldSupply){
          foreach($oldSupply AS $oldOne){            
            $oldRummage = $oldRummage + $oldOne->getRummage();
            $oldPerPrice = $oldPerPrice + $oldOne->getPerPrice();
            $oldTotal = $oldTotal + $oldOne->getTotal();

            $oldOne->setIsActive(0);
            $em = $this->getDoctrine()->getManager();
            $em->persist($oldOne);
            $em->flush();
          }
        }
        
        $total = $per_price[$k] * $quantity[$k];
        
        $supply = new Supply();
        $supply->setShop($shop);
        $supply->setProductKey($oneProductKey);
        $supply->setPerPrice($per_price[$k]);
        $supply->setTotal($oldTotal + $total);
        $supply->setSupQuantity($quantity[$k]);
        $supply->setRummage($oldRummage + $quantity[$k]);//anxnii uldegdel
        $supply->setCreatedDate(new \DateTime);
        $supply->setCreatedUserId(1);
        $supply->setUpdatedUserId(1);
        $supply->setIsActive(1);

        $em = $this->getDoctrine()->getManager();
        $em->persist($supply);
        $em->flush();
        
      }
      
      $this->get('session')->setFlash('notice', $shop->getName() . ' Дэлгүүрт '.$oneProductKey->getProduct()->getName().' Бүтээгдэхүүнийг амжилттай нийлүүллээ.');
      return $this->redirect($this->generateUrl('showSupply'));
      
      
    }
}
